<?php
    require_once 'dbConn.php';
    date_default_timezone_set("PRC");
     
     
    $name = $_POST['name'];
    $pic = $_POST['pic'];
    $price = $_POST['price'];
    $number = $_POST['number'];
    $email = $_POST['email'];
  
    //查询用户是否已经添加了该商品
	$select_sql = "select * from user_shop where pic = '$pic' and userid = (select id from user where email = '$email')";	

    //给商品总数+1
    $update_sql = "update user_shop set number =  number + 1 where  pic = '$pic' and userid = (select id from user where email = '$email')"; 

    //添加新的商品
    $insert_sql = "insert into user_shop(name,pic,price,number,userid) values('$name','$pic',$price,$number,(select id from user where email = '$email'))"; 
    
     
	// 执行sql语句
    mysql_query($select_sql,$con);
    // 获取影响的行数
    $select_rows = mysql_affected_rows();

    // 返回影响行数
    // 如果影响行数>=1 就代表有重复的商品  就执行修改商品件数操作
  if($select_rows >= 1){
  	
   	// 执行sql语句
    mysql_query($update_sql,$con);
    // 获取影响的行数
    $update_rows = mysql_affected_rows();
    
    if($update_rows >= 1){
     	echo true;
    }else{
     	echo false;
    }
     
  }else{
  	
    // 执行sql语句
    mysql_query($insert_sql,$con);
    // 获取影响的行数
    $insert_rows = mysql_affected_rows();
    
    if($insert_rows >= 1){
     	echo true;
    }else{
     	echo false;
    }
    
  }
?>